home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6597 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  64 lines

  1. Newsgroups: comp.lang.c
  2. Path: nntp.coast.net!torn!sq!msb
  3. From: msb@sq.com (Mark Brader)
  4. Subject: Re: Tradition or what?
  5. Message-ID: <1996Feb14.233911.5533@sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <1996Feb13.115611.73989@cc.usu.edu>
  8. Date: Wed, 14 Feb 1996 23:39:11 GMT
  9.  
  10. Erik van Renselaar (erik@cc.usu.edu) writes:
  11. > Can anyone tell me what the use is of returning
  12. > the same value for the function and one of the
  13. > output parameters, like it is done in strcpy?
  14.  
  15. strcpy() doesn't do that, but does something even less useful -- it
  16. returns a copy of one of its input arguments.  (This argument happens to
  17. point to an area in which output is written, hence Erik's misstatement).
  18.  
  19. In the case of the string functions, returning a pointer to the buffer
  20. permits expressions like
  21.  
  22.     strcat (strcat (strcat (strcpy (buf, s1), s2), s3), s4);
  23.  
  24. without needing to mention buf four times.  That's about it.  Nowadays
  25. I think most people would prefer to write that on four separate lines,
  26. mentioning buf four times, or (as I would) to use
  27.  
  28.     sprintf (buf, "%s%s%s%s", s1, s2, s3, s4);
  29.  
  30. instead; and some of us wish that all these functions (including sprintf())
  31. had been designed to return a pointer to the terminating '\0' that they
  32. write in the string.
  33.  
  34. An instance where the output really is returned two ways is time():
  35.  
  36.     time_t x, y;
  37.     x = time (&y);
  38.  
  39. Both x and y get the same value.  In this case, the reason is that
  40. the call time() existed on UNIX in an era when int, which was 16 bits,
  41. was the widest integer type, so there wasn't a numeric type that could
  42. serve as time_t does now.  In those days time() was used like *this*:
  43.  
  44.     int z[2];
  45.     time (z);
  46.  
  47. If prototypes had existed then, time()'s would have been void time(int *).
  48. Later when long was available, time() was made to return its value for
  49. the sake of convenience, but it was also desired for the traditional 
  50. call to continue working so that code using it didn't need to be fixed.
  51. Note that functions like ctime() take a time_t * rather than a time_t;
  52. the same reasons apply.
  53.  
  54. Or at least, this is my understanding.  I wasn't there.
  55. -- 
  56. Mark Brader, msb@sq.com, SoftQuad Inc., Toronto
  57.         Until 3,000 million years ago we can say not a lot happened
  58.         although further study would not come amiss.  Then signs of life
  59.         appeared, including some large reptiles and, very recently, bipeds.
  60.         It is too soon to say whether these bipeds will play an important
  61.         part in the world's story.    -- Colin Morris in "History Today"
  62.  
  63. My text in this article is in the public domain.
  64.